﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SERP.DAO;
using SERP.Controllers;
using System.Data;
using SERP;
namespace SERP.Modules.HRM.Controls
{
    [System.Web.UI.ValidationProperty("ValidationText")]
    public partial class DropDownListGender : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindGender();
            }
        }
        public event EventHandler SelectedIndexChanged;
        protected void drd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChanged != null)
            {
                SelectedIndexChanged(sender, e);
            }
        }
        public bool AppendDataBoundItems
        {
            get
            {
                return drd.AppendDataBoundItems;
            }
            set
            {
                drd.AppendDataBoundItems = value;
            }
        }
        public string AccessKey
        {
            get
            {
                return drd.AccessKey;
            }
            set
            {
                drd.AccessKey = value;
            }
        }
        public System.Web.UI.AttributeCollection Attributes
        {
            get
            {
                return drd.Attributes;
            }
        }
        public bool AutoPostBack
        {
            get
            {
                return drd.AutoPostBack;
            }
            set
            {
                drd.AutoPostBack = value;
            }
        }
        public Color BackColor
        {
            get
            {
                return drd.BackColor;
            }
            set
            {
                drd.BackColor = value;
            }
        }
        public Color BorderColor
        {
            get
            {
                return drd.BorderColor;
            }
            set
            {
                drd.BorderColor = value;
            }
        }
        public System.Web.UI.WebControls.BorderStyle BorderStyle
        {
            get
            {
                return drd.BorderStyle;
            }
            set
            {
                drd.BorderStyle = value;
            }
        }
        public string CssClass
        {
            get
            {
                return drd.CssClass;
            }
            set
            {
                drd.CssClass = value;
            }
        }
        public string DataMember
        {
            get
            {
                return drd.DataMember;
            }
            set
            {
                drd.DataMember = value;
            }
        }
        public object DataSource
        {
            get
            {
                return drd.DataSource;
            }
            set
            {
                drd.DataSource = value;
            }
        }
        public string DataSourceID
        {
            get
            {
                return drd.DataSourceID;
            }
            set
            {
                drd.DataSourceID = value;
            }
        }
        public string DataTextField
        {
            get
            {
                return drd.DataTextField;
            }
            set
            {
                drd.DataTextField = value;
            }
        }
        public string DataTextFormatString
        {
            get
            {
                return drd.DataTextFormatString;
            }
            set
            {
                drd.DataTextFormatString = value;
            }
        }
        public string DataValueField
        {
            get
            {
                return drd.DataValueField;
            }
            set
            {
                drd.DataValueField = value;
            }
        }
        private bool _IsAddChooseItem = false;
        private bool _IsAddAllItem = false;
        private string _SelectedValue = "";
        public bool IsAddChooseItem
        {
            set
            {
                _IsAddChooseItem = value;
            }
            get
            {
                return _IsAddChooseItem;
            }
        }
        public bool IsAddAllItem
        {
            set
            {
                _IsAddAllItem = value;
            }
            get
            {
                return _IsAddAllItem;
            }
        }
        //Bind data to txtGender
        public void BindGender()
        {
            DataTable tblGender = new DataTable();
            tblGender.Columns.Add("ID", typeof(int));
            tblGender.Columns.Add("Name", typeof(string));
            if (IsAddAllItem)
            {
                DataRow r = tblGender.NewRow();
                r["ID"] = -1;
                r["Name"] = Constants.APP_ALL;
                tblGender.Rows.Add(r);
            }
            if (IsAddChooseItem)
            {
                DataRow r = tblGender.NewRow();
                r["ID"] = -1;
                r["Name"] = Constants.CHOOSE_ITEM;
                tblGender.Rows.Add(r);
            }
            DataRow row = tblGender.NewRow();
            row["ID"] = 0;
            row["Name"] = Constants.APP_FEMALE;
            tblGender.Rows.Add(row);
            row = tblGender.NewRow();
            row["ID"] = 1;
            row["Name"] = Constants.APP_MALE;
            tblGender.Rows.Add(row);
            this.drd.DataTextField = "Name";
            this.drd.SelectedValue = null;
            this.drd.DataValueField = "ID";
            this.drd.DataSource = tblGender;
            this.drd.DataBind();
            if (_SelectedValue == "")
            {
                if (this.drd.Items.Count > 0)
                {
                    this.drd.SelectedIndex = 0;
                }
            }
            else
            {
                if (drd.Items.FindByValue(_SelectedValue) != null)
                {
                    this.drd.SelectedValue = _SelectedValue;
                }
                else
                {
                    if (this.drd.Items.Count > 0)
                    {
                        this.drd.SelectedIndex = 0;
                    }
                }
            }
        }
        public string SelectedValue
        {
            get
            {
                return drd.SelectedValue;
            }
            set
            {
                _SelectedValue = value;
                drd.SelectedValue = value;
            }
        }
        public int SelectedIndex
        {
            get
            {
                return drd.SelectedIndex;
            }
            set
            {
                drd.SelectedIndex = value;
            }
        }
        [System.ComponentModel.Browsable(false)]
        public string ValidationText
        {
            get
            {
                if (_IsAddChooseItem)
                {
                    if (drd.SelectedIndex <= 0)
                    {
                        return string.Empty;
                    }
                }
                else
                {
                    if (drd.SelectedIndex < 0) return string.Empty;
                }
                return drd.Text;
            }
        }
        public System.Web.UI.WebControls.Unit Width
        {
            get
            {
                return drd.Width;
            }
            set
            {
                drd.Width = value;
            }
        }
    }
}
